﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_DeliveryTerm : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string delivery_id;

        public frm_Edit_DeliveryTerm()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.delivery_id = null;
        }

        /////////////////////////////////////////
        /// Load Data to Form
        /////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(delivery_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT DeliveryTermID, DeliveryTermName, ExOrIm, HandOverPlace"
                        + " FROM [222_DeliveryTerm] WHERE DeliveryTermID='" + delivery_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.deliveryTermID.Text = dataReader["DeliveryTermID"].ToString();
                        this.deliveryTermName.Text = dataReader["DeliveryTermName"].ToString();
                        if ((bool)dataReader["ExOrIm"])
                        {
                            this.importOrExport.Checked = true;
                        }
                        else
                        {
                            this.importOrExport.Checked = false;
                        }
                        this.handOver.Text = dataReader["HandOverPlace"].ToString();
                    }
                    else
                    {
                        ret = false;
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                    //MessageBox.Show(ex.Message);
                }
            }
            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string deliveryterm_id = null;
            string deliveryterm_name = null;
            int export = 0;
            string handover = null;
            
            if (string.IsNullOrEmpty((this.deliveryTermID.Text)))
            {
                error = "Please enter Delivery Term ID";
            }
            else
            {
                deliveryterm_id = this.deliveryTermID.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.deliveryTermName.Text))
                {
                    error = "Please enter Delivery Term Name";
                }
                else
                {
                    deliveryterm_name = this.deliveryTermName.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.importOrExport.Checked)
                {
                    export = 1;
                }
                else
                {
                    export = 0;
                }

                handover = this.handOver.Text;

                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_DeliveryTerm] SET DeliveryTermID='" + deliveryterm_id + "',"
                        + " DeliveryTermName='" + deliveryterm_name + "', ExOrIm=" + export + ", HandOverPlace='" + handover + "'"
                        + " WHERE DeliveryTermID='" + delivery_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Delivery Term added success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Delivery Term. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
